<template>
    <div>
        <div style="display: flex;justify-content: center;margin-top: 10px">
            <el-input v-model="keywords" placeholder="通过用户名搜索用户..." size="small" prefix-icon="el-icon-search" style="width: 400px"></el-input>
            <el-button type="primary" @click="doSearch" size="small" style="margin-left: 10px" icon="el-icon-search">搜索</el-button>
        </div>

        <div class="admin-container">
            <el-card class="admin-card" v-for="(admin,index) in admins" :key="index">
                <div slot="header" class="clearfix">
                    <span>{{admin.name}}</span>
                    <el-button icon="el-icon-delete" @click="deleteAdmin(admin)" style="float: right; padding: 3px 0 ;color: red" type="text"></el-button>
                </div>
                <div>
                    <div style="width: 100%;display: flex;justify-content: center">
                        <img src=""  class="userface-img">
                    </div>
                </div>
                <div class="userinfo-container">
                    <div>用户名：{{admin.name}}</div>
                    <div>手机号码：{{admin.phone}}</div>
                    <div>电话号码：{{admin.telephone}}</div>
                    <div>地址：{{admin.address}}</div>
                    <div>
                        用户状态:
                        <el-switch
                                v-model="admin.enabled"
                                active-color="#13ce66"
                                inactive-color="#ff4949"
                                @change="enabledChange(admin)"
                                active-text="禁用"
                                inactive-text="启用">
                        </el-switch>
                    </div>

                </div>
                <div class="userinfo-container">
                    用户角色：
                    <el-tag type="success" style="margin-right: 4px" v-for="(role,index) in admin.roles" :key="index">{{role.nameZh}}</el-tag>
                    <el-popover
                            placement="right"
                            title="角色列表"
                            width="200"
                            @show="showRoles(admin)"
                            @hide="updatePop(admin)"
                            trigger="click">
                        <el-select v-model="selectRoles" multiple placeholder="请选择">
                            <el-option
                                    v-for="(r,index) in allRoles"
                                    :key="index"
                                    :label="r.nameZh"
                                    :value="r.id">
                            </el-option>
                        </el-select>
                        <el-button slot="reference"  type="text" icon="el-icon-more"></el-button>
                    </el-popover>

                </div>
                <div class="userinfo-container">
                    备注：{{admin.remark}}
                </div>
            </el-card>
        </div>

    </div>


</template>

<script>
    export default {

        name: "SysAdmin",
        data(){
            return{
                admins:[],
                keywords:'',
                allRoles:[],
                selectRoles:[]

            }
        },
        mounted(){
            this.initAdmins();
        },
        methods:{
            updatePop(admin){
                let roles = [];
                Object.assign(roles,admin.roles)
                let flag = false;
                if(roles.length!=this.selectRoles.length){
                    flag = true;
                }else {
                    for (let i = 0; i < roles.length; i++) {
                        let role = roles[i];
                        for (let j = 0; j < this.selectRoles.length; j++) {
                            let sr = this.selectRoles[j];
                            if(role.id==sr){
                                roles.splice(i,1);
                                i--;
                                break;
                            }
                        }
                    }
                    if(roles.length!=0){
                        flag = true
                    }
                }



                if(flag){
                    let url = '/system/admin/role?adminId='+ admin.id;
                    this.selectRoles.forEach(sr=>{
                        url+='&rids='+sr;
                    })
                    this.putRequest(url).then(resp=>{
                        if(resp){
                            this.initAdmins();
                        }
                    })
                }



            },
            showRoles(admin){
                let roles =admin.roles;
                this.selectRoles =[];
                roles.forEach(r=>{
                    this.selectRoles.push(r.id);
                })
                this.initAllRoles();
            },
            initAllRoles(){
              this.getRequest('/system/admin/roles').then(resp=>{
                  if(resp){
                      this.allRoles=resp;
                  }
              })
            },
            enabledChange(admin){
                this.putRequest('/system/admin/',admin).then(resp=>{
                    if(resp){
                        this.initAdmins();
                    }
                })
            },
            deleteAdmin(admin){
                this.$confirm('是否永久删除'+admin.name+'操作员, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/admin/'+admin.id).then(resp=>{
                        if(resp){
                            this.initAdmins();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消删除'
                    });
                });
            },
            doSearch(){
                this.initAdmins();
            },
            initAdmins(){
                this.getRequest('/system/admin/?keywords='+this.keywords).then(resp=>{
                    if(resp){
                        this.admins =resp;
                    }
                })

            }
        }
    }
</script>

<style >
    .admin-container{
        display: flex;
        margin-top: 10px;
        justify-content: space-around;
        /*自动换行*/
        flex-wrap: wrap;

    }
    .admin-card{
        width: 350px;
        margin-bottom: 20px;
    }
    .userface-img{
        width: 72px;
        height: 72px;
        border-radius: 72px;
    }
    .userinfo-container{
        font-size: 12px;
        color: #1accff;
    }
</style>